package stz.flash.main.strobe.model;

import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
import stz.flash.main.ManagerOfManager;
import stz.flash.main.ScreenHandle;
import stz.flash.main.state.KeyOperator;
import stz.flash.option.Option;
import stz.flash.option.OptionScreen;


public class GlobalKeyListenerOfMainApp implements KeyListener {

	public boolean keyDown(int keycode, int time) {
		
		if (checkKeyWhenHold(keycode) || isUnavailableState() || 
				Keypad.key(keycode) == Keypad.KEY_CAMERA_FOCUS)
			return false;
		
		KeyOperator.analyzeGlobalKey(Keypad.key(keycode));
		
		return false;
	}
	
	private boolean checkKeyWhenHold(int keycode) {
		
		if (ManagerOfManager.isDisplayedHoldManager() == true &&
				Keypad.key(keycode) == Option.getIns().getStrobeToggleKey()) 
			return true;
		
		return false;
	}
	
	private boolean isUnavailableState() {
		return (ScreenHandle.getIns().isDisplayed() == false && 
				Strobe.getThread().isAlive() == false) ||
				OptionScreen.isOptionDisplayed();
	}
	
	public boolean keyChar(char key, int status, int time) { return false; }
	public boolean keyRepeat(int keycode, int time) { return false; }
	public boolean keyStatus(int keycode, int time) { return false; }
	public boolean keyUp(int keycode, int time) { return false; }
}